# +--------------------------
# | User: zq                -
# | Version: python3.7      -
# | Time: 2020-03-12 09:31                
# +--------------------------
# 多线程编程
# 1. 实例化 Thread
# 2. 继承Tread类

import time
from threading import Thread


def sleep_task(sleep_time):
    print("sleep {} second start!".format(sleep_time))
    time.sleep(sleep_time)
    print("sleep {} seconds end!".format(sleep_time))


# 进程类
class SleepThread(Thread):
    def __init__(self, sleep_time):
        self.sleep_time = sleep_time
        super().__init__()

    def run(self):
        print("sleep {} seconds start!".format(self.sleep_time))
        time.sleep(self.sleep_time)
        print("sleep {} seconds end!".format(self.sleep_time))


if __name__ == "__main__":
    start_time = time.time()
    t1 = SleepThread(2)
    t1.setDaemon(True)
    t2 = SleepThread(3)
    t2.setDaemon(True)
    t1.start()
    t2.start()


    end_time = time.time()
    print("last_time:{}".format(end_time - start_time))

# if __name__ == "__main__":
#     start_time = time.time()
#     t1 = Thread(target=sleep_task, args=(2,))
#     t1.setDaemon(True)  # 守护线程,主线程退出,子线程也退出
#     t1.start()
#
#     t2 = Thread(target=sleep_task, args=(3,))
#     t2.setDaemon(True)  # 守护线程,主线程退出,子线程也退出
#     t2.start()
#
#     # 加了join之后,主线程会等到 t1,t2结束之后再继续执行下去
#     # t1.join()
#     # t2.join()
#     time.sleep(1)
#
#     # 现在有一个新需要,主线程给 t1,t2线程 1秒时间内完成,不然就关闭
#
#     end_time = time.time()
#     print("last_time:{}".format(end_time - start_time))
#
#     # 1. 当开启一个程序的时候,会默认启动一个主线程
#     # 2. 如果在主线程等到其他线程执行完以后才继续执行, join
